package handler

import (
	"github.com/gin-gonic/gin"
	"pet-manager/models"
	"pet-manager/pkg"
)

func UserList(c *gin.Context) {
	uid, _ := c.Get("uid")
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"uid":  uid,
	})
	return
}
func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	var user models.User
	models.DB.Where("username = ?", username).First(&user)
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.MD5(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	token := pkg.TokenHandler(int(user.ID))
	c.JSON(200, gin.H{
		"code":  500,
		"msg":   "success",
		"token": token,
		"data":  user,
	})
	return
}
